package logic

import (
	"context"
	"errors"
	"srv/models/mysqlinit"

	"srv/internal/svc"
	"srv/srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseCoverLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseCoverLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseCoverLogic {
	return &HouseCoverLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseCoverLogic) HouseCover(in *srv.HouseCoverRequest) (*srv.HouseCoverResponse, error) {
	// todo: add your logic here and delete this line
	h := mysqlinit.House{}
	err := h.FindHouse(in.HouseId)
	if err != nil {
		logx.Error("获取房源详情失败")
		return nil, errors.New("获取房源详情失败")
	}
	if h.ID == 0 {
		logx.Error("房源不存在")
		return nil, errors.New("房源不存在")
	}
	hc := mysqlinit.HouseCover{}
	err = hc.FindHouseCover(in.HouseId)
	if err != nil {
		logx.Error("获取房源图片详情失败")
		return nil, errors.New("获取房源图片详情失败")
	}
	if hc.ID != uint(in.HouseId) {
		err = hc.CreateHouseCover()
		if err != nil {
			logx.Error("添加加图片失败")
			return nil, errors.New("添加加图片失败")
		}
	}
	hc = mysqlinit.HouseCover{

		HouseId: int(in.HouseId),
		Living:  in.Living,
		Sleep:   in.Sleep,
		Kitchen: in.Kitchen,
		Toilet:  in.Toilet,
	}

	mysqlinit.DB.Debug().Where("id = ?", in.HouseId).Updates(&hc)

	return &srv.HouseCoverResponse{
		HouseId: int64(hc.ID),
	}, nil
}
